package cn.b.sky.shop.activiti.controller;

import cn.b.sky.shop.activiti.model.ProcessModelCategory;
import cn.b.sky.shop.activiti.model.ProcessModelParam;
import cn.b.sky.shop.activiti.service.ProcessModelCategoryService;
import cn.b.sky.shop.activiti.service.ProcessParamService;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("param")
public class ProcessParamController extends SecurityController {
  @Resource
  private ProcessModelCategoryService processModelCategoryService;

  @Resource
  private ProcessParamService processParamService;

  @Resource
  private ECodeService eCodeService;

  /**
   * 模型树
   */
  @RequestMapping("paramList")
  public String toParamList(Model model, String navTabId) {
    List<ProcessModelCategory> pmc = processModelCategoryService.getProcessModelCategoryTree(this.getSiteCode());
    model.addAttribute("processModel", pmc);
    model.addAttribute("navTabId", navTabId);
    return "params/process_params";
  }

  @RequestMapping("getParamsData")
  public String paramsList(Model model, String navTabId, String modelId) {

    if (StringUtils.isBlank(modelId)) {
      throw ImeException.newException("parameterNull.M");
    }
    List<ProcessModelParam> pmp = processParamService.getProcessModeParamByModelId(modelId);
    model.addAttribute("processModelParams", pmp);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("modelId", modelId);
    return "params/paramsDetail";
  }

  @RequestMapping("addParam")
  public String addPMP(Model model, String navTabId, String modelId) {
    if (StringUtils.isBlank(modelId)) {
      throw ImeException.newException("parameterNull.M");
    }
    List<ECode> ecodes = this.eCodeService.selectECodeList(ECodeEnums.PARAMS_DATATYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("dataType", ecodes);
    model.addAttribute("modelId", modelId);
    model.addAttribute("navTabId", navTabId);
    return "params/params_add";
  }

  @RequestMapping("saveProcessModelParam")
  @ResponseBody
  public DWZResult saveProcessModelParam(Model model, HttpServletRequest request, String navTabId,
                                         ProcessModelParam param) {
    if (param == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }

    if (StringUtils.isBlank(param.getCode()) || StringUtils.isBlank(param.getModelId())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }
    processParamService.saveProcessModel(param);
    String forwardUrl = getForwardUrl(request) + "/param/getParamsData?modelId=" + param.getModelId() + "&navTabId="
        + navTabId;
    return dialogAjaxDone(navTabId, null, forwardUrl, null);
  }

  @RequestMapping("isExitsParam")
  @ResponseBody
  public boolean isExitsConfigCategory(String modelId, String code, Model model) {
    if (!StringUtils.isBlank(modelId) && !StringUtils.isBlank(code)) {
      ProcessModelParam pp = processParamService.getProcessModeParamByModelIdAndCode(modelId, code);
      if (pp != null && !StringUtils.isBlank(pp.getCode())) {
        return false;
      }
    }
    return true;
  }

  @RequestMapping("rmProcessModelParam")
  @ResponseBody
  public DWZResult rmProcessModelParam(Model model, HttpServletRequest request, String navTabId, String id,
                                       String modelId) {
    if (StringUtils.isBlank(id) || StringUtils.isBlank(modelId)) {

      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }
    processParamService.deleteProcessModelByPK(modelId, id);
    model.addAttribute("modelId", modelId);
    model.addAttribute("navTabId", navTabId);
    String forwardUrl = getForwardUrl(request) + "/param/getParamsData?modelId=" + modelId + "&navTabId=" + navTabId;
    return dialogAjaxDone(navTabId, null, forwardUrl, null);
  }
}
